package apis

import (
	"xiaoercaidan/baseApi/dao"
	"xiaoercaidan/publicResources/controller"
	"xiaoercaidan/publicResources/models"
	"time"
	"xiaoercaidan/publicResources/utils"
	"strconv"
	"encoding/json"
	"github.com/goinggo/mapstructure"
)

type TableApi struct {
	controller.BaseController
}


// @Title InserTable
// @Description 新增桌台
// @Param   table     patch string   true string       "桌台json"
// @Success 200 {object} models.Table
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /inserTable [get]
func (c *TableApi) InserTable() {
	var BaseResult = new(models.BaseResult)

	var table = new(models.Table)
	var tableStr = c.GetString("table")

	if ( len(tableStr) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数不能为空"
	} else {

		var mapStr map[string]interface{}

		json.Unmarshal([]byte(tableStr), &mapStr)

		for k := range mapStr {
			if k == "Id" {
				mapStr[k] = nil
			}
		}

		mapstructure.Decode(mapStr, table);


		if (table == nil || table.ShopId <= 0) {
			BaseResult.Code = 100
			BaseResult.Msg = "店铺ID不能为空"
		} else {

			table.CreateTime = time.Now().UTC().Unix()
			table.QRcode = "{\"ShopId\": \"" + strconv.FormatInt(table.ShopId, 10) + "\",\"TableId\": \"" + strconv.FormatInt(table.TableId, 10) + "\",\"TableName\": \"" + table.TableName + "\",\"TableSize\": \"" + strconv.FormatInt(table.TableSize, 10) + "\",\"Status\": \"" + strconv.Itoa(table.Status) + "\",}"

			err := dao.InsertTable(table)

			if (utils.CheckResult(BaseResult, err)) {
				BaseResult.Data = table
			}

		}

	}

	c.Data["json"] = &BaseResult
	c.ServeJSON()

}


// @Title GetTable
// @Description 查询单个桌台
// @Param   tableId     patch string   true int64       "桌号"
// @Param   shopId    patch string  true  int64       "店铺号"
// @Success 200 {object} models.Table
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /getTable [get]
func (c *TableApi) GetTable() {

	var tableId = c.GetString("tableId")
	var shopId = c.GetString("shopId")

	var BaseResult = new(models.BaseResult)

	if ( len(shopId) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "店铺ID不能为空"
	} else {

		var params = make(map[string]string)

		params["shop_id"] = shopId

		if (len(tableId) > 0) {
			params["table_id"] = tableId
		}

		tables := dao.QeryTableBlur(params)

		if (tables != nil && len(*tables) > 0) {
			BaseResult.Data = tables
		} else {
			BaseResult.Code = 101
			BaseResult.Msg = "您查询的桌台不存在"
		}

	}
	c.Data["json"] = &BaseResult
	c.ServeJSON()

}

// @Title TableDel
// @Description 删除桌台
// @Param   shopId int64 "店铺id"
// @Param   tableId  string   "桌台ID列表， 以,分割"
// @Success 200 {object} models.Table
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /tableDel [get]
func (this *TableApi) TableDel() {

	var BaseResult = new(models.BaseResult)
	var tableId = this.GetString("tableId")
	var shopId = this.GetString("shopId")

	if ( len(shopId) <= 0 || len(tableId) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "店铺ID和桌台ID不能为空"
	} else {

		err := dao.DeleteTables(shopId, tableId)

		if (err != nil) {
			BaseResult.Code = 100
			BaseResult.Msg = err.Error()
		} else {
			BaseResult.Msg = "删除成功"
		}

	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()

}

// @Title TableUpdate
// @Description 修改桌台
// @Param  table   patch models.Table  true  string       桌台数据
// @Success 200 {object} models.Table
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /tableUpdate [get]
func (this *TableApi) TableUpdate() {

	var BaseResult = new(models.BaseResult)

	var table = new(models.Table)

	var tableJson = this.GetString("table")

	if ( len(tableJson) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"

	} else {

		json.Unmarshal([]byte(tableJson), &table)

		if (table != nil && table.Id > 0) {
			err := dao.UpdateTable(table)

			if (utils.CheckResult(BaseResult, err)) {
				BaseResult.Data = table
			}
		} else {
			BaseResult.Code = 100
			BaseResult.Msg = "没有查询到相关结果"

		}

	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()

}

